﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 回复帖子的回复信息
    /// </summary>
    public class CBizRecoverPosts : CustBizObject
    {
        private CDmDjkRecoverPosts m_CDmDjkRecoverPosts = new CDmDjkRecoverPosts();
        private CDmDjkRecoverPosts oDmDjkRecoverPosts
        {
            get { return m_CDmDjkRecoverPosts; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizRecoverPosts()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkRecoverPosts.DJK_RECOVER_POSTS);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["RECOVER_POSTS_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }

            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "RECOVER_POSTS_ID" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }

            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            //new msBsc.AppLogic.DataAlc.CBizSourcePosts().UpdateSourcePosts(drw["RECOVER_TO_POSTS"].ToString(), drw["RECOVER_BY"].ToString());
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加回复信息
        /// </summary>
        /// <param name="recoverContent">回复内容</param>
        /// <param name="recoverBy">回复者编号</param>
        /// <param name="recoverTo">回复帖子编号</param>
        /// <returns></returns>
        public string[] AddRecoverPosts(string recoverContent, string recoverBy, string recoverTo, string sourceId)
        {
            #region 内容基本判断
            if (string.IsNullOrEmpty(sourceId))
            {
                return new string[] { "FALSE", "原帖不能为空" };
            }
            if (string.IsNullOrEmpty(recoverContent))
            {
                return new string[] { "FALSE", "回复内容不能为空" };
            }
            if (string.IsNullOrEmpty(recoverBy))
            {
                return new string[] { "FALSE", "回复者编号不能为空" };
            }
            if (string.IsNullOrEmpty(recoverTo))
            {
                return new string[] { "FALSE", "回复帖子编号不能为空" };
            }
            if (recoverContent.Length > 1000)
            {
                return new string[] { "FALSE", "回复内容过长" };
            }
            #endregion

            //回复帖子信息表
            DataSet ds = new CDmDjkRecoverPosts();
            DataRow dr = ds.Tables[0].NewRow();
            dr["RECOVER_BY"] = recoverBy;
            dr["REVOCER_CONTENT"] = recoverContent;
            dr["RECOVER_TO_POSTS"] = recoverTo;

            #region 判断回复类型并设置信息
            //int isRecoverSource = IsRecoverSource(recoverTo);
            using (CBizSourcePosts objSourcePosts1 = new CBizSourcePosts())
            {
                DataSet ds1 = objSourcePosts1.GetPostsByRecoverId(sourceId);
                DataRow dr1 = ds1.Tables[0].Rows[0];
                dr1["POSTS_RECOVER_COUNT"] = Convert.ToInt32(dr1["POSTS_RECOVER_COUNT"]) + 1;
                dr1["POSTS_HAS_NEW_RECD"] = 1;
                if (recoverTo == sourceId)
                {
                    dr["IS_REVOCER_SOURCE"] = 1;
                    dr["RECOVER_LEVEL"] = GetRecoverLevel(recoverTo, true) + 1;
                    dr1["POSTS_LAST_RECOVER"] = recoverBy;
                }
                else
                {
                    dr["IS_REVOCER_SOURCE"] = 0;
                }
                objSourcePosts1.Update(ds1);
            }
            dr["REVOCER_TIME"] = this.dtCurrDateTime;
            dr["RECOVER_POSTS_ID"] = this.DataObj.NewID();
            dr["HAS_NEW_RECORD"] = 1;
            dr["POSTS_IS_DELETED"] = 0;
            dr["POSTS_IS_DISPLAY"] = 1;
            dr["POSTS_RMK"] = null;
            #endregion
            ds.Tables[0].Rows.Add(dr);

            this.Update(ds);
            return new string[] { "OK", "回复成功" };
        }

        #region 查询是否为回复原帖
        /// <summary>
        /// 判断回复的是否为原帖
        /// </summary>
        /// <param name="postsId">帖子编号</param>
        /// <returns></returns>
        public int IsRecoverSource(string postsId)
        {
            if (string.IsNullOrEmpty(postsId))
            {
                return -1;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("POSTSID", ParameterDirection.Input, EDbType.String, 36, postsId.Replace("'", "").Replace(",", "','")));
            DataSet ds = this.DataObj.QueryBySQL(@"SELECT POSTS_RECOVER_COUNT FROM DJK_SOURCE_POSTS WHERE SOURCE_POSTS_ID=@POSTSID", parmCollection);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return 0;
            }
            DataRow dr = ds.Tables[0].Rows[0];

            return dr["POSTS_RECOVER_COUNT"] is DBNull ? 0 : Convert.ToInt32(dr["POSTS_RECOVER_COUNT"]);
        }
        #endregion

        #region 查询帖子点击量
        /// <summary>
        /// 查询帖子点击量
        /// </summary>
        /// <param name="postsId">帖子编号</param>
        /// <returns></returns>
        public int IsPostsClickCount(string postsId)
        {
            if (string.IsNullOrEmpty(postsId))
            {
                return -1;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("POSTSID", ParameterDirection.Input, EDbType.String, 36, postsId.Replace("'", "").Replace(",", "','")));
            DataSet ds = this.DataObj.QueryBySQL("SELECT POSTS_CLICK_COUNT FROM DJK_SOURCE_POSTS WHERE SOURCE_POSTS_ID=@POSTSID", parmCollection);
            ds.Tables[0].TableName = this.TableName;
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return 0;
            }
            DataRow dr = ds.Tables[0].Rows[0];
            return dr["POSTS_CLICK_COUNT"] is DBNull ? 0 : Convert.ToInt32(dr["POSTS_CLICK_COUNT"]);
        }
        #endregion

        #region 查询楼层
        /// <summary>
        /// 查询回复楼层
        /// </summary>
        /// <param name="postsId">帖子编号</param>
        /// <param name="isSource">是否为回复原帖</param>
        /// <returns>楼层数</returns>
        private int GetRecoverLevel(string postsId, bool isSource)
        {
            if (string.IsNullOrEmpty(postsId))
            {
                return -1;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("POSTSID", ParameterDirection.Input, EDbType.String, 36, postsId.Replace("'", "").Replace(",", "','")));

            if (isSource)
            {
                DataRow dr = this.DataObj.QueryBySQL(@"SELECT COUNT(1) RECOVER_LEVEL FROM DJK_RECOVER_POSTS WHERE       RECOVER_TO_POSTS=@POSTSID", parmCollection).Tables[0].Rows[0];
                return dr["RECOVER_LEVEL"] is DBNull ? 0 : Convert.ToInt32(dr["RECOVER_LEVEL"]);
            }
            else
            {
                DataRow dr = this.DataObj.QueryBySQL(@"SELECT RECOVER_LEVEL FROM DJK_RECOVER_POSTS WHERE RECOVER_POSTS_ID=@POSTSID", parmCollection).Tables[0].Rows[0];
                return dr["RECOVER_LEVEL"] is DBNull ? 0 : Convert.ToInt32(dr["RECOVER_LEVEL"]);
            }
        }
        #endregion


    }
}
